package com.example.system.Service.question.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.domain.R;
import com.example.common.core.domain.ResultCode;
import com.example.common.core.domain.TableDataInfo;
import com.example.security.exception.ServiceException;
import com.example.system.Service.question.IQuestionService;
import com.example.system.domain.question.Question;
import com.example.system.domain.question.dto.QuestionAddDTO;
import com.example.system.domain.question.dto.QuestionEditDTO;
import com.example.system.domain.question.dto.QuestionQueryDTO;
import com.example.system.domain.question.vo.QuestionDetailVo;
import com.example.system.domain.question.vo.QuestionVO;
import com.example.system.mapper.QuestionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class QuestionService implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * @param questionQueryDTO
     * @return
     */
    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        List<QuestionVO> questionVOList = questionMapper.selectQuestionList(questionQueryDTO);
        if (questionVOList == null || questionVOList.isEmpty()) {
            return TableDataInfo.empty();
        }
        log.info("service-questionVOList{}",questionVOList);
        return TableDataInfo.success(questionVOList, new PageInfo<>(questionVOList).getTotal());
    }

    @Override
    public Integer add(QuestionAddDTO questionAddDTO) {
        //确认题目title是否重复
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        if (questionList.size() > 0) {
            throw new ServiceException(ResultCode.FAILED_ADD_QUESTION_FOR_REPEATED_TITLE);
        }
        Question question = new Question();
        //处理前端的入参
        BeanUtil.copyProperties(questionAddDTO, question);
        //处理题目创建人
        question.setCreateBy(1L);
        //处理时间
        question.setCreateTime(LocalDateTime.now());

        return questionMapper.insert(question);
    }

    @Override
    public QuestionDetailVo getQuestionDetailById(Long questionId) {
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .ne(Question::getDeleteFlag, 0).eq(Question::getQuestionId, questionId));
        if (questions == null) {
            //question不能为空
            throw new ServiceException(ResultCode.FAILED_GET_QUESTION);
        } else if (questions.size() != 1) {
            //一个questionId只能对应一条数据 查出多条 显然是不合理的
            throw new ServiceException(ResultCode.FAILED_GET_QUESTION);
        }
        log.info("service-getQuestionDetailById:{}", questions.get(0));
        QuestionDetailVo questionDetailVo = new QuestionDetailVo();
        BeanUtil.copyProperties(questions.get(0), questionDetailVo);
        log.info("service-getQuestionDetailById:{}", questionDetailVo);
        return questionDetailVo;
    }

    /****
     *首先从数据库通过id来查这个题目
     * 修改
     * 提交至数据库
     * @param questionEditDTO
     * @return
     *   title;
     *   difficulty;
     *   timeLimit;
     *   spaceLimit;
     *   content;
     *   questionCase;
     *   defaultCode;
     *   mainFunc;
     */
    @Override
    public R<Void> editQuestionById(QuestionEditDTO questionEditDTO) {
        Question question = questionMapper.selectById(questionEditDTO.getQuestionId());
        if (question == null) {
            throw new ServiceException(ResultCode.FAILED_EDIT_QUESTION);
        }
        question.setTitle(questionEditDTO.getTitle());
        question.setDifficulty(questionEditDTO.getDifficulty());
        question.setTimeLimit(questionEditDTO.getTimeLimit());
        question.setSpaceLimit(questionEditDTO.getSpaceLimit());
        question.setContent(questionEditDTO.getContent());
        question.setQuestionCase(questionEditDTO.getQuestionCase());
        question.setDefaultCode(questionEditDTO.getDefaultCode());
        question.setMainFunc(questionEditDTO.getMainFunc());
        int updateRet = questionMapper.updateById(question);
        if (updateRet != 1) {
            throw new ServiceException(ResultCode.FAILED_EDIT_QUESTION);
        }
        return R.ok(ResultCode.SUCCES_EDIT_QUESTION);
    }

    @Override
    public R<Void> deleteQuestionById(Long questionId) {
        return null;
    }
}
